public class ThreadAdvantage {
    //多线程的优势，运行速度快
    private static final long count =100000000;

    public static void main(String[] args) throws InterruptedException {
        //
        concurrency();
        //
        serial();
    }
    private static void concurrency() throws InterruptedException {
        //用于返回当前时间的纳秒数
        long begin=System.nanoTime();
        Thread thread=new Thread(new Runnable() {
            @Override
            public void run() {
                int a=0;
                for(long i=0;i<count;i++){
                    a--;
                }
            }
        });
        thread.start();

        int b=0;
        for(long i=0;i<count;i++){
            b--;
        }
        thread.join();
        long end=System.nanoTime();
        double ms=(end-begin)*1.0/1000/1000;
        System.out.printf("并发: %f 毫秒%n", ms);

    }
    private static void serial() {
        long begin = System.nanoTime();
        int a = 0;
        for (long i = 0; i < count; i++) {
            a--;
        }
        int b = 0;
        for (long i = 0; i < count; i++) {
            b--;


        }
        long end = System.nanoTime();
        double ms = (end - begin) *1.0/1000/1000;
        System.out.printf("串行: %f 毫秒%n", ms);
    }
}
